<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11 0011
 * Time: 下午 4:36
 */
namespace app\core\controller;
use app\admin\controller\Admin;
use think\Db;
class Server extends Admin{
    /**
     * 当前模块参数
     */
    protected function _infoModule(){
        return array(
            'info'  => array(
                'name' => '关于我们管理',
                'description' => '管理关于我们',
            ),
            'menu' => array(
                array(
                    'name' => '关于我们列表',
                    'url' => url('serverList'),
                )
            ),
            '_info' => array(
                array(
                    'name' => '添加关于我们',
                    'url' => url('serverAdd'),
                ),
            ),
        );
    }

    //关于我们列表
    public function serverList(){
        $list = Db::name('core_server')->where($this->condition(input()))->paginate(10);
        $this->view->list = $list->all();
        $this->view->page = $list->render();
        return $this->fetch();
    }

    //关于我们添加、修改
    public function serverAdd(){
        if($this->request->isPost()){
            $data = input('post.');
            if($data['id']){
                $content = '地区修改';$status = 2;
                Db::name('core_server')->where('id',input('id'))->update($data);
            }else{
                $content = '地区添加';$status = 1;
                if(Db::name('core_server')->where('name',$data['name'])->find() || Db::name('core_server')->where('number',$data['number'])->find()){
                    return ajaxReturn(500,'当前地区编号或名称存在');
                }
                $data['create_time'] = time();
                Db::name('core_server')->insert($data);
            }
            $this->log($content,$status);
            return ajaxReturn(200,'操作成功',url('serverList'));
        }
        $this->view->info = Db::name('core_server')->where('id',input('id'))->find();
        return $this->fetch();
    }

    //删除
    public function del(){
        $content = '地区删除';$status = 3;
        if(Db::name('core_area')->where('pid',input('id'))->find()){
            return ajaxReturn(500,'当前地区下存在小区，请删除后操作');
        }else{
            Db::name('core_server')->delete(input('id'));
        }
        $this->log($content,$status);
        return ajaxReturn(200,'删除成功');
    }

    /**
     * 索引条件
     * @param   $data   提交数据
     */
    protected function condition($data){
        $where = '';
        if(!empty($data['number'])){
            $where['a.number'] = $data['number'];
        }
        if(!empty($data['name'])){
            $where['a.name'] = array('like','%'.$data['name'].'%');
        }
        if(!empty($data['status'])){
            $where['a.status'] = $data['status'];
        }
        return $where;
    }
}